%% Load
clear;
close;

[name path] = uigetfile({'*.GTB'});
Head = file_Load(name, path, 'Head');
Data = file_Load(Head.FileName, Head.FilePath, 'Data');
clear name path;
%% View

Fig_ScrollData(Head, Data);


%% Get spindle sample
Data = util_DataReference(Data,Head.RefeChan);

Event = Head.Event;
n_event = length(Event.Time);

stime = Head.StartDate+Head.StartTime;
fs = Head.SampRate;
chan = util_GetIndex(Head.ChanLabel, Event.Channel);

wsize = 1 * fs + 1;

sample = zeros(n_event,wsize);

for e = 1 : n_event
    idx = util_GetOffIndex(stime,fs,Event.Time(e));
    sample(e,:) = Data(chan,idx-floor(wsize/2):idx+floor(wsize/2));
end

%% Sample Figure
i = 1;

for f = 1 : ceil(n_event/50)
    
    h = figure('outerposition',[1281 0 1920 1080]);
    for s = 1 : 50
        subplot(5,10,s);
        plot(sample(i,:));
        set(gca,'ydir','reverse');
        xlim([1 201]);
        ylim([-1000 1000]);
        title(['S ' num2str(i)]);
        i = i + 1;
        
        if i > n_event
            break;
        end
    end
    
%     saveas(h, ['S' num2str(f, '% .2d')], 'bmp')
    close;
    
end

%% Relative Power
i = 1;
L = size(sample,2);
nfft = lgt_nfft(L);
win = window('hann', L);
fbin = util_GetFreqBin(fs, L);
freq = fbin(util_GetIndex(fbin,1):util_GetIndex(fbin,20));
fidx = util_GetIndex(fbin,freq);
P = zeros(n_event, length(fidx));

for f = 1 : ceil(n_event/50)
    
    h = figure('outerposition',[1281 0 1920 1080]);
    for s = 1 : 50
        subplot(5,10,s);
        X = win' .* sample(i,:);
        Y = power(abs(lgt_fft(X,fs,2)),2);
        Y = util_GetRelative(Y(fidx), 2);
        plot(freq,Y);hold on;
        line([4 4],[0 1], 'color','k', 'linestyle',':');
        line([8 8],[0 1], 'color','k', 'linestyle',':');
        line([12 12],[0 1], 'color','r', 'linestyle',':');
        line([15 15],[0 1], 'color','r', 'linestyle',':');
        line([0 20],[0.15 0.15], 'color','k', 'linestyle',':');
        xlim([freq(1) freq(end)]);
        ylim([0 0.5]);
        title(['P ' num2str(i)]);
        P(i,:) = Y;
        i = i + 1;
        
        if i > n_event
            break;
        end        
    end
    
%     saveas(h, ['P' num2str(f, '% .2d')], 'bmp')
    close;
    
end


%% TF Figure
i = 1;
fbin = util_GetFreqBin(fs, size(sample,2));
freq = fbin(util_GetIndex(fbin,1):util_GetIndex(fbin,20));
E = zeros(n_event, length(fidx), size(sample,2));

for f = 1 : ceil(n_event/50)
    
    h = figure('outerposition',[1281 0 1920 1080]);
    for s = 1 : 50
        subplot(5,10,s);
        Y = abs(lgt_cwt(sample(i,:)',fs,freq,5,'gab','same'));
        Y = util_GetRelative(Y, 1);
        imagesc([],freq,Y)
        caxis([0 0.3]);
        title(['E ' num2str(i)]);
        E(i,:,:) =  Y;
        i = i + 1;
        
        if i > n_event
            break;
        end         
    end    
    saveas(h, ['E' num2str(f, '% .2d')], 'bmp')
    close;
    
end



%% LS-SVM (Multi-class coding)
t1 = [3,7,8,9,19,22,24,96,124,192,254,309,322,334,347,359,415,421,514,...
    524,558,662,765,809,875,1085,1214,1485,1536,1513,1571,1675,1665,1770,...
    1726,1926,2056,2236,2251,2265,2330,2350,2384,2461];
t2 = [1,2,4,5,6,10,12,14,15,16,17,20,21,26,27,28,30,32,34,35,36,37,39,47,...
    49,50,51,58,59,61,62,63,65,69,79,80,81,85,86,87,88,91,93,95,100];

clear X y Y;
X(1:length(t1),:) = P(t1,:);
X(length(t1)+1:length(t1)+length(t2),:) = P(t2,:);
y(1:length(t1)) = 1;
y(length(t1)+1:length(t1)+length(t2)) = 2;

model = initlssvm(X,y','c',[],[],'RBF_kernel');
model = tunelssvm(model,'simplex','crossvalidatelssvm',{10,'misclass'},'code_OneVsAll');
model = trainlssvm(model);


Y = simlssvm(model,P);



